/**
 * Project: springyounger.lottery.dal
 * 
 * File Created at 2010-3-27
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyounger.lottery.dal.dao.ibatis;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.springyounger.lottery.dal.dao.QuestionDao;
import com.springyounger.lottery.dal.dataobject.LotteryPhaseQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestion;
import com.springyounger.lottery.dal.dataobject.LotteryQuestionResult;

/**
 * QuestionDaoImpl
 * 
 * @author springyounger
 */
public class QuestionDaoImpl extends SqlMapClientDaoSupport implements QuestionDao {

    public LotteryPhaseQuestion getPhaseQuestion(LotteryPhaseQuestion question) {
        return (LotteryPhaseQuestion) getSqlMapClientTemplate().queryForObject("getPhaseQuestion",
                question);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryQuestionResult> getQuestionResult(int questionId) {
        return (List<LotteryQuestionResult>) getSqlMapClientTemplate().queryForList(
                "getResutlByQuestionId", new Integer(questionId));
    }

    public LotteryQuestionResult getQuestionResultByResultId(int resultId) {
        return (LotteryQuestionResult) getSqlMapClientTemplate().queryForObject(
                "getResultByResultId", new Integer(resultId));
    }

    @SuppressWarnings("unchecked")
    public LotteryPhaseQuestion getPhaseQuestion(int questionId,int phaseId) {
        Map params = new HashMap();
        params.put("questionId", questionId);
        params.put("phaseId", phaseId);
        return (LotteryPhaseQuestion) getSqlMapClientTemplate().queryForObject(
                "getPhaseQuestionByQIdAndPhId", params);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryQuestion> getAllQuestion(int pageIndex, int pageSize) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        return (List<LotteryQuestion>) getSqlMapClientTemplate().queryForList("getAllQuestion",
                params);
    }

    public int getAllQuestionCount() {
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllQuestionCount");
    }

    public int deleteQuestionById(int questionId) {
        return getSqlMapClientTemplate().delete("deleteQuestionById", questionId);
    }

    public int updateQuestion(LotteryQuestion question) {
        return getSqlMapClientTemplate().update("updateQuestion", question);
    }

    public LotteryQuestion getQuestionById(int questionId) {
        return (LotteryQuestion) getSqlMapClientTemplate().queryForObject("getQuestionById",
                questionId);
    }

    public String getResultById(int questionId) {
        return (String) getSqlMapClientTemplate().queryForObject("getResultById", questionId);
    }

    public LotteryQuestion insertQuestion(LotteryQuestion question) {
        getSqlMapClientTemplate().insert("insertQuestion", question);
        return question;
    }

    public int updateQuestionResult(LotteryQuestionResult questionResult) {
        return getSqlMapClientTemplate().update("updateQuestionReslut", questionResult);
    }

    public void batchInsertQuestionResult(final List<LotteryQuestionResult> resultList,
                                          final int questionId) {
        //批量插入 
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (LotteryQuestionResult result : resultList) {
                    result.setQuestionId(questionId);
                    //调用ibatis
                    executor.insert("insertQuestionReslut", result);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    public void batchUpdateQuestionResult(final List<LotteryQuestionResult> resultList,
                                          final int questionId) {
        //批量更新
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (LotteryQuestionResult result : resultList) {
                    result.setQuestionId(questionId);
                    //调用ibatis
                    executor.insert("updateQuestionReslut", result);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    public int isExistName(String content) {
        return (Integer) getSqlMapClientTemplate().queryForObject("getQuestionByContent", content);
    }

    public void batchInsertPhaseQuestion(final List<LotteryPhaseQuestion> phaseQuestionList) {
        //批量更新
        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (LotteryPhaseQuestion phaseQuestion : phaseQuestionList) {
                    //调用ibatis
                    executor.insert("insertPhaseQeustion", phaseQuestion);
                }
                executor.executeBatch();
                return null;
            }
        });
    }

    @SuppressWarnings("unchecked")
    public List<LotteryQuestion> getAllQuestionByType(String questionType) {
        return (List<LotteryQuestion>) getSqlMapClientTemplate().queryForList(
                "getAllQuestionByType", questionType);
    }

    public int getAllQuestionCountByType(String questionType) {
        return (Integer) getSqlMapClientTemplate().queryForObject("getAllQuestionCountByType",
                questionType);
    }

    public int getPhaseQuestionCount(int phaseId) {
        return (Integer) getSqlMapClientTemplate().queryForObject("getPhaseQuestionCount", phaseId);
    }

    @SuppressWarnings("unchecked")
    public List<LotteryPhaseQuestion> getAllPhaseQuestion(int phaseId, int pageIndex, int pageSize) {
        Map params = new HashMap();
        pageIndex = pageIndex * pageSize;
        if (pageIndex < 0) {
            pageIndex = 0;
        }
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        params.put("phaseId", phaseId);
        return (List<LotteryPhaseQuestion>) getSqlMapClientTemplate().queryForList(
                "getAllPhaseQuestion", params);
    }
}
